package com.example.androidclass;

import android.content.ClipData;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
    private ArrayList<OneDay> day_list;
    private ArrayList<ArrayList<OneActivity>> all_activity_list;
    private Context context;

    public MyBaseExpandableListAdapter(ArrayList<OneDay> day_list, ArrayList<ArrayList<OneActivity>> all_activity_list, Context context) {
        this.day_list = day_list;
        this.all_activity_list = all_activity_list;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return day_list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return all_activity_list.get(groupPosition).size();
    }

    @Override
    public OneDay getGroup(int groupPosition) {
        return day_list.get(groupPosition);
    }

    @Override
    public OneActivity getChild(int groupPosition, int childPosition) {
        return all_activity_list.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup groupHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.one_day_template, parent, false);
            groupHolder = new ViewHolderGroup();
            groupHolder.day_of_the_week = (TextView) convertView.findViewById(R.id.day_of_the_week);
            groupHolder.date = (TextView) convertView.findViewById(R.id.date);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (ViewHolderGroup) convertView.getTag();
        }
        groupHolder.day_of_the_week.setText(day_list.get(groupPosition).getDay());
        groupHolder.date.setText(day_list.get(groupPosition).getDate());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        if(convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.one_activity_template, parent, false);
            itemHolder = new ViewHolderItem();
            itemHolder.class_name = (TextView) convertView.findViewById(R.id.class_name);
            itemHolder.classroom = (TextView) convertView.findViewById(R.id.classroom);
            itemHolder.time = (TextView) convertView.findViewById(R.id.time);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();
        }
        itemHolder.class_name.setText(all_activity_list.get(groupPosition).get(childPosition).getClass_name());
        itemHolder.classroom.setText(all_activity_list.get(groupPosition).get(childPosition).getClassroom());
        itemHolder.time.setText(all_activity_list.get(groupPosition).get(childPosition).getTime());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolderGroup{
        private TextView day_of_the_week;
        private TextView date;
    }

    private static class ViewHolderItem{
        private TextView class_name;
        private TextView classroom;
        private TextView time;
    }
}

















